package com.punch.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punch.common.dto.system.TopTeacherDTO;
import com.punch.common.exception.BusinessException;
import com.punch.common.page.PageInfo;
import com.punch.common.query.syste.TopTeacherQueryInfo;
import com.punch.common.simple.redis.utils.UserUtil;
import com.punch.common.tools.BasePage;
import com.punch.common.utils.ImageUtil;
import com.punch.common.vo.common.OptionVO;
import com.punch.common.vo.system.TopTeacherVO;
import com.punch.system.exception.SysExceptionEnum;
import com.punch.system.mapper.TopTeacherMapper;
import com.punch.system.model.TopTeacher;
import com.punch.system.service.TopTeacherService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.StringJoiner;

/**
 * <p>
 * 名师表 服务实现类
 * </p>
 *
 * @author wjj
 * @since 2020-12-14
 */
@Service
public class TopTeacherServiceImpl extends ServiceImpl<TopTeacherMapper, TopTeacher> implements TopTeacherService {

    /**
     * 名师列表
     *
     * @param queryInfo 查询条件
     * @return PageInfo<TopTeacherVO>
     */
    @Override
    public PageInfo<TopTeacherVO> listTopTeacher(TopTeacherQueryInfo queryInfo) {
        queryInfo.setSortStr("sort_by:asc");
        return BasePage.queryBasePage(queryInfo, params -> this.getBaseMapper().queryCount(params),
                params -> {
                    List<TopTeacherVO> retList = getBaseMapper().queryList(params);
                    retList.forEach(topTeacherVO -> {
                        if (topTeacherVO.getStatus() == 0) {
                            topTeacherVO.setUpdateTime(null);
                        }
                    });
                    return retList;
                });
    }

    /**
     * 保存名师
     *
     * @param topTeacherDTO 提交表单
     * @return id
     */
    @Override
    public Integer saveTopTeacher(TopTeacherDTO topTeacherDTO) {
        TopTeacher topTeacher = new TopTeacher();
        BeanUtil.copyProperties(topTeacherDTO, topTeacher);
        Long userId = UserUtil.getLoginUserId();
        topTeacher.setCreateId(userId);
        //处理名师图片前缀
        if (StrUtil.isNotEmpty(topTeacherDTO.getTeacherImg())) {
            String img = ImageUtil.removePrefix(topTeacherDTO.getTeacherImg());
            topTeacher.setTeacherImg(img);
        }
        //处理介绍图片
        if (CollUtil.isNotEmpty(topTeacherDTO.getImageList())) {
            StringJoiner sj = new StringJoiner(",");
            topTeacherDTO.getImageList().forEach((img) -> sj.add(ImageUtil.removePrefix(img)));
            topTeacher.setIntroduceImg(sj.toString());
        } else {
            topTeacher.setIntroduceImg("");
        }
        if (!save(topTeacher)) {
            throw new BusinessException(SysExceptionEnum.DATA_INSERT_EXCEPTION);
        }
        return topTeacher.getId();
    }

    /**
     * 修改名师
     *
     * @param topTeacherDTO 提交表单
     * @return id
     */
    @Override
    public Integer updateTopTeacher(TopTeacherDTO topTeacherDTO) {
        TopTeacher topTeacher = new TopTeacher();
        BeanUtil.copyProperties(topTeacherDTO, topTeacher);
        //处理介绍图片
        if (CollUtil.isEmpty(topTeacherDTO.getImageList())) {
            topTeacher.setIntroduceImg("");
        } else {
            StringJoiner sj = new StringJoiner(",");
            topTeacherDTO.getImageList().forEach((img) -> sj.add(ImageUtil.removePrefix(img)));
            topTeacher.setIntroduceImg(sj.toString());
        }
        //处理名师介绍图片前缀
        if (StrUtil.isNotEmpty(topTeacherDTO.getTeacherImg())) {
            String img = ImageUtil.removePrefix(topTeacherDTO.getTeacherImg());
            topTeacher.setTeacherImg(img);
        }
        topTeacher.setId(topTeacherDTO.getTeacherId());
        if (!updateById(topTeacher)) {
            throw new BusinessException(SysExceptionEnum.DATA_UPDATE_EXCEPTION);
        }
        return topTeacher.getId();
    }

    /**
     * 名师详情
     *
     * @param teacherId teacherId
     * @return TopTeacherVO
     */
    @Override
    public TopTeacherVO getTopTeacher(Integer teacherId) {
        TopTeacher topTeacherEX = getById(teacherId);
        Optional.ofNullable(topTeacherEX).orElseThrow(() -> new BusinessException(SysExceptionEnum.DATA_NOT_EXIST));
        TopTeacherVO topTeacherVO = new TopTeacherVO();
        BeanUtil.copyProperties(topTeacherEX, topTeacherVO);
        //讲堂介绍
        if (StrUtil.isNotEmpty(topTeacherEX.getIntroduceImg())) {
            String[] split = topTeacherEX.getIntroduceImg().split(",");
            List<String> imgList = new ArrayList<>();
            for (String img : split) {
                imgList.add(ImageUtil.addPrefix(img));
            }
            topTeacherVO.setImageList(imgList);
        }
        //讲堂封面图片
        if (StrUtil.isNotEmpty(topTeacherEX.getTeacherImg())) {
            topTeacherVO.setTeacherImg(ImageUtil.addPrefix(topTeacherEX.getTeacherImg()));
        }
        topTeacherVO.setTeacherId(teacherId);
        return topTeacherVO;
    }

    /**
     * 删除名师
     *
     * @param teacherId teacherId
     * @return id
     */
    @Override
    public Integer deleteTopTeacher(Integer teacherId) {
        TopTeacher topTeacherEX = getById(teacherId);
        Optional.ofNullable(topTeacherEX).orElseThrow(() -> new BusinessException(SysExceptionEnum.DATA_NOT_EXIST));
        //已经展示的不能删除,需要下架
        if (topTeacherEX.getStatus() == 1) {
            throw new BusinessException(SysExceptionEnum.STATUS_ERROR);
        }
        if (!removeById(teacherId)) {
            throw new BusinessException(SysExceptionEnum.DATA_DELETION_EXCEPTION);
        }
        return teacherId;
    }

    /**
     * 修改名师状态
     *
     * @param teacherId teacherId
     * @param status    状态 0-不显示 1-显示
     * @return teacherId
     */
    @Override
    public Integer updateTopTeacherStatus(Integer teacherId, Integer status) {
        TopTeacher topTeacherEX = getById(teacherId);
        Optional.ofNullable(topTeacherEX).orElseThrow(() -> new BusinessException(SysExceptionEnum.DATA_NOT_EXIST));
        if (topTeacherEX.getStatus().equals(status)) {
            throw new BusinessException(SysExceptionEnum.STATUS_ERROR);
        }
        TopTeacher topTeacher = new TopTeacher();
        topTeacher.setStatus(status);
        topTeacher.setId(teacherId);
        if (!updateById(topTeacher)) {
            throw new BusinessException(SysExceptionEnum.DATA_UPDATE_EXCEPTION);
        }
        return teacherId;
    }

    /**
     * 简单名师列表
     *
     * @return List<OptionVO < Integer>>
     */
    @Override
    public List<OptionVO<Integer>> listSimpleTeacher() {
        List<OptionVO<Integer>> retList = new ArrayList<>();
        list().forEach((t) -> {
            OptionVO<Integer> optionVO = new OptionVO<>();
            optionVO.setValue(t.getId());
            optionVO.setLabel(t.getTeacherName());
            retList.add(optionVO);
        });
        return retList;
    }
}
